﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier_Service;
using Cashier_Model;
using Cashier.utils;
using System.Collections;

namespace Cashier.orgs
{
    public partial class StoreInfoAddForm : Form
    {
        private MainForm mainform;
        private IReloadData iReloadData;
        private StoreInfoService storeInfoService;
        private String storeNo;
        private Int64 storeInfoId;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="iReloadData"></param>
        /// <param name="storeInfoId"></param>
        /// <param name="storeNo"></param>
        public StoreInfoAddForm(MainForm mainform, IReloadData iReloadData, Int64 storeInfoId, String storeNo)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.iReloadData = iReloadData;
            this.storeInfoService = new StoreInfoService();
            this.storeNo = storeNo;
            this.storeInfoId = storeInfoId;

            if (String.IsNullOrEmpty(storeNo))
            {
                this.Text = "新增门店信息";
            }
            else
            {
                this.Text = "编辑门店信息";
            }
            this.InitialFormData();
        }

        #region 功能方法

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        protected void InitialFormData()
        {
            if (String.IsNullOrEmpty(storeNo))
            {
                textbox_storeNo.Text = GuidService.GetGUID("storeInfo").ToString();
            }
            else
            {
                this.Text = "编辑门店信息";
                textbox_storeNo.Enabled = false;
                StoreInfo storeInfo = this.storeInfoService.GetStoreInfo(0, storeNo);
                if (storeInfo != null)
                {
                    FormUtil.InitialFormData(storeInfo, this.groupBox1, 1);
                }
            }
        }

        /// <summary>
        /// 保存门店信息
        /// </summary>
        protected void SaveStoreInfo()
        {
            StoreInfo storeInfo = this.storeInfoService.GetStoreInfo(0, textbox_storeNo.Text.Trim());
            if (storeInfo != null && storeInfo.Id != storeInfoId)
            {
                MessageBox.Show("门店编号重复，建议使用系统生成！");
                return;
            }

            if (storeInfo == null)
            {
                storeInfo = new StoreInfo();
            }
            storeInfo.StoreNo = textbox_storeNo.Text.Trim();
            storeInfo.Name = textbox_name.Text.Trim();
            storeInfo.Contact = textbox_contact.Text.Trim();
            storeInfo.Mobile = textbox_mobile.Text.Trim();
            storeInfo.Tel = textbox_tel.Text.Trim();
            storeInfo.Fax = textbox_fax.Text.Trim();
            storeInfo.Email = textbox_email.Text.Trim();
            storeInfo.Address = textbox_address.Text.Trim();
            storeInfo.Zip = textbox_zip.Text.Trim();
            storeInfo.Note = textbox_note.Text.Trim();

            int flag = 0;
            if (storeInfoId > 0)
            {
                flag = this.storeInfoService.UpdateStoreInfo(storeInfo);
            }
            else
            {
                storeInfo.EnterpriseNo = mainform.Admin.EnterpriseNo;
                storeInfo.CreateTime = DateTime.Now;
                storeInfoId = this.storeInfoService.InsertStoreInfo(storeInfo);
                if (storeInfoId > 0)
                {
                    flag = 1;
                }
            }

            if (flag > 0)
            {
                MessageBox.Show("保存成功！");
                iReloadData.ReloadDataGridViewData(null);
            }
            else
            {
                MessageBox.Show("保存成功！");
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this.groupBox1, list, 1);
            if (list.Count > 0)
            {
                return;
            }
            this.SaveStoreInfo();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}
